<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Post extends CI_Controller {

    public function index() {
    }

    public function post_message() {
        $this->load->model('MessagesModel');
        $this->load->model('PostsModel');
        $message = trim($this->input->post('message'));

        if ($message == "") return;

        //save message
        $this->MessagesModel->postMessage($message);

        //return the post in HTML (as AJAX response)
        $id = $this->db->insert_id();
        $post = $this->PostsModel->getPosts($id);
        $this->load->view("posts/post_template", array("post" => $post));
    }

    public function post_poll() {
        $this->load->model('PollsModel');
        $this->load->model('PostsModel');


        $question = $this->input->post('question');
        $answers = $this->input->post('answer');

        $this->PollsModel->addPoll($question, $answers);

        //return the post in HTML (as AJAX response)
        $id = $this->db->insert_id();
        $post = $this->PostsModel->getPosts($id);
        $this->load->view("posts/post_template", array("post" => $post));
    }


    public function post_event() {
        $this->load->model('EventsModel');
        $this->load->model('PostsModel');
        $this->load->model('PollsModel');

        $this->EventsModel->createEvent();


        //return the post in HTML (as AJAX response)
        $id = $this->db->insert_id();
        $post = $this->PostsModel->getPosts($id);
        $this->load->view("posts/post_template", array("post" => $post));
    }

    public function delete($id) {
        $this->load->model("PostsModel");

        $post = $this->PostsModel->getPosts($id);

        if ($post->user_id == $this->tank_auth->get_user_id() && time() - $post->date_posted_timestamp < 300) {
            $this->PostsModel->deletePost($id);
        } else {
            echo "Acced denied.";
        }
    }

}